//
//  DSPvistorView.swift
//  SWweibo
//
//  Created by 杜守鹏 on 15/12/28.
//  Copyright © 2015年 杜守鹏. All rights reserved.
//

import UIKit
import SnapKit

class DSPvistorView: UIView {
    
    var didLoginButton:(()->())?
    var didregisterButton:(()->())?
    lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy var circleImageVIew:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var coverImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    lazy  var textLabel:UILabel = {
     
        let textLabel = UILabel()
        
        textLabel.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        textLabel.numberOfLines = 0
        
        textLabel.textAlignment = .Center
        
        textLabel.textColor = UIColor.grayColor()
        
        textLabel.font = UIFont.systemFontOfSize(14)
        return textLabel
        
    }()
    
    lazy var loginButton:UIButton = {
       
        let button =  UIButton()
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
  button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.addTarget(self, action: "didClickLogin", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    lazy var registerButton:UIButton = {
       let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.addTarget(self , action: "didClickregisterButton", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
   @objc private func didClickLogin(){
     didLoginButton?()
    }
    
  @objc private  func didClickregisterButton(){
        didregisterButton?()
    }
    override  init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupInfo(imageName:String?,labelContent:String?){
        if imageName == nil{
        zhuanquan()
        }else{
        circleImageVIew.hidden = true
        textLabel.text = labelContent
       iconImageView.image = UIImage(named: imageName!)
        }
    }
    
    private func zhuanquan(){
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        animation.toValue = 2 * M_PI
        animation.removedOnCompletion = false
        circleImageVIew.layer.addAnimation(animation, forKey: nil)
        
    }
    
    func setupUI(){
        backgroundColor = UIColor(white: 237/255, alpha: 1)
      addSubview(circleImageVIew)
         addSubview(coverImageView)
        addSubview(iconImageView)
        addSubview(loginButton)
        addSubview(registerButton)
          addSubview(textLabel)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(snp_center)
        }
//        iconImageView.translatesAutoresizingMaskIntoConstraints = false
//      
//        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        circleImageVIew.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconImageView.snp_center)
        }
//        circleImageVIew.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: circleImageVIew, attribute: .CenterY, relatedBy: .Equal, toItem: iconImageView, attribute: .CenterY, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: circleImageVIew, attribute: .CenterX, relatedBy: .Equal, toItem: iconImageView, attribute: .CenterX, multiplier: 1, constant: 0))
//  
        coverImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleImageVIew.snp_center)
        }
//        coverImageView.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: coverImageView, attribute: .CenterX, relatedBy: .Equal, toItem: circleImageVIew, attribute: .CenterX, multiplier: 1, constant: 0 ))
//        addConstraint(NSLayoutConstraint(item: coverImageView, attribute: .CenterY, relatedBy: .Equal, toItem: circleImageVIew, attribute: .CenterY, multiplier: 1, constant: 0))
        
        textLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(coverImageView.snp_leading)
            make.top.equalTo(coverImageView.snp_bottom).offset(16)
            make.width.equalTo(175)
        }
//        
//        textLabel.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: textLabel, attribute: .Leading, relatedBy: .Equal, toItem: coverImageView, attribute: .Leading, multiplier: 1, constant: 0))
//    
//        addConstraint(NSLayoutConstraint(item: textLabel, attribute: .Top, relatedBy: .Equal, toItem: iconImageView, attribute: .Bottom, multiplier: 1, constant: 16))
//        
//        addConstraint(NSLayoutConstraint(item: textLabel, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1, constant: 175))
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(textLabel.snp_leading)
           make.top.equalTo(textLabel.snp_bottom)
            make.width.equalTo(80)
        }
//        registerButton.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: textLabel, attribute: .Leading, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: textLabel, attribute: .Bottom, multiplier: 1, constant: 16));
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1, constant: 80))
 loginButton.snp_makeConstraints { (make) -> Void in
    make.top.equalTo(registerButton.snp_top)
    make.width.equalTo(80)
    make.trailing.equalTo(textLabel.snp_trailing)
        }
//        loginButton.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1, constant: 80))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: .Equal, toItem: textLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        
    }
}
